package com.pagepub.expo.action.corp;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.StringTokenizer;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;
import com.pagepub.comm.util.FileUploadUtil;
import com.pagepub.expo.entity.CorpCommpany;
import com.pagepub.expo.entity.CorpProducts;
import com.pagepub.expo.entity.IdName;
import com.pagepub.expo.manager.AppSeqMng;
import com.pagepub.expo.manager.CorpCommpanyMng;
import com.pagepub.expo.manager.CorpProductsMng;

@Controller
public class CorpProductsAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	

	@Autowired
	@Qualifier("corpProductsMng")
	private CorpProductsMng corpProductsMng;

	@Autowired
	@Qualifier("appSeqMng")
	private AppSeqMng appSeqMng;

	public List<CorpProducts> rows = new ArrayList<CorpProducts>();

	public int start = 0;
	private boolean success;

	public boolean isSuccess() {
		return success;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}

	public long getNewId() {
		return newId;
	}

	public void setNewId(long newId) {
		this.newId = newId;
	}

	public int limit = 0;

	public long newId = 0;

	private List<IdName> ids;

	public List<IdName> getIds() {
		return ids;
	}

	public void setIds(List<IdName> ids) {
		this.ids = ids;
	}
	
	private File smallPic;

	private File bigPic;

	private String smallPicFileName; // 上传的文件名 (1.系统自动注入 2.变量命名有规则:
	// 前台对象名+"FileName")
	private String bigPicFileName; // 上传的文件名 (1.系统自动注入 2.变量命名有规则:
	// 前台对象名+"FileName")
	

	public CorpProductsAction() {
		System.out.println("this is CorpProductsAction");
	}

	public File getSmallPic() {
		return smallPic;
	}

	public void setSmallPic(File smallPic) {
		this.smallPic = smallPic;
	}

	public File getBigPic() {
		return bigPic;
	}

	public void setBigPic(File bigPic) {
		this.bigPic = bigPic;
	}

	public String getSmallPicFileName() {
		return smallPicFileName;
	}

	public void setSmallPicFileName(String smallPicFileName) {
		this.smallPicFileName = smallPicFileName;
	}

	public String getBigPicFileName() {
		return bigPicFileName;
	}

	public void setBigPicFileName(String bigPicFileName) {
		this.bigPicFileName = bigPicFileName;
	}

	public String execute() throws Exception {
		System.out.println("this is execute");
		return null;
	}

	public String all() {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		int start = Integer.valueOf(request.getParameter("start"));
		int limit = Integer.valueOf(request.getParameter("limit"));
		CorpCommpany corpCommpany = (CorpCommpany) session.getAttribute("corpCommpany");
		int currentPage = start;
		int pageSize = limit;
		setNull();
		rows = corpProductsMng.findByPage(currentPage, pageSize, corpCommpany.getId());
		this.limit = corpProductsMng.getCount(null);
		this.start = start;
		success = true;
		return SUCCESS;
	}

	public String newId() {
		HttpServletRequest request = ServletActionContext.getRequest();
		String seqName = request.getParameter("seqName").trim();
		if (seqName == null || seqName.equals("")) {
			newId = appSeqMng.getDefault();
		} else {
			newId = appSeqMng.getSequence(seqName);
		}
		success = true;
		return SUCCESS;
	}

	public List<CorpProducts> getRows() {
		return rows;
	}

	public void setRows(List<CorpProducts> rows) {
		this.rows = rows;
	}

	public int getLimit() {
		return limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public String delete() {
		HttpServletRequest request = ServletActionContext.getRequest();
		String ids = request.getParameter("delIds");
		ids = ids.substring(0, ids.length() - 1);
		StringTokenizer st = new StringTokenizer(ids, ",", false);
		List<String> StringList = new ArrayList<String>();
		while (st.hasMoreElements()) {
			StringList.add(st.nextToken());
		}
		for (int i = 0; i < StringList.size(); i++) {
			corpProductsMng.remove(new Long(StringList.get(i)));
		}
		setNull();
		success = true;
		return SUCCESS;
	}

	public String add() {
		uploadFile();

		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		CorpCommpany corpCommpany = (CorpCommpany) session
				.getAttribute("corpCommpany");
		corpProducts.setCorpId(corpCommpany.getId().toString());

		corpProducts.setAddTime(new Date());
		corpProducts.setIsAuditing(0);
		corpProductsMng.add(corpProducts);
		setNull();
		uploadFileResponse();
		return SUCCESS;
	}

	public String modify() {
		uploadFile();
		
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		CorpCommpany corpCommpany = (CorpCommpany) session
				.getAttribute("corpCommpany");
		corpProducts.setCorpId(corpCommpany.getId().toString());

		
		corpProducts.setUpdateTime(new Date());
		corpProductsMng.update(corpProducts);
		setNull();
		uploadFileResponse();
		return SUCCESS;
	}

	public String ids() {
		setNull();
		ids = corpProductsMng.findIdNames();
		success = true;
		return SUCCESS;
	}

	private CorpProducts corpProducts;

	public CorpProducts getCorpProducts() {
		return corpProducts;
	}

	public void setCorpProducts(CorpProducts corpProducts) {
		this.corpProducts = corpProducts;
	}
	
	private void setNull(){
		ids=null;
		smallPic=null;
		corpProducts=null;
		rows=null;
	}

	private void uploadFile(File file, String fileName) {
		String  root = ServletActionContext.getServletContext().getRealPath("/upload/products");
		String path = root+"\\"+corpProducts.getCorpId();
		FileUploadUtil.uploadFile(file, fileName, path);
	}
	
	
	private void uploadFile(){
		if(smallPic!=null){
			smallPicFileName=System.currentTimeMillis()%11973+(int)(Math.random()*1297)+smallPicFileName;
			uploadFile(smallPic,smallPicFileName);
			corpProducts.setSmallPic(smallPicFileName);
		}
		if(bigPic!=null){
			bigPicFileName=System.currentTimeMillis()%11973+(int)(Math.random()*1297)+bigPicFileName;
			uploadFile(bigPic,bigPicFileName);
			corpProducts.setBigPic(bigPicFileName);
		}
	}

	
	
	private void uploadFileResponse(){
		try {
			ServletActionContext.getResponse().getWriter().write("{success:true}");
			ServletActionContext.getResponse().setContentType("text/html");
			ServletActionContext.getResponse().getWriter().close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	

}
